<?php

/**
 * Class FriendshipController
 */
class FriendshipController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getFriends($user_id)
    {
        $nerds = Frelation::where('fr_relation.user_id', '=', $user_id)
            ->join('profile', 'fr_relation.friend_id', '=', 'profile.user_id')
            ->select('profile.user_id','profile.fullname','profile.profile_picture')
            ->orderBy('profile.fullname', 'desc')
            ->paginate(5);

        return Response::json(array('status' => 'success', 'state' => $nerds->toArray()), 200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function getFollowers($user_id)
    {
        $nerds = Frelation::where('fr_relation.friend_id', '=', $user_id)
            ->join('profile', 'fr_relation.user_id', '=', 'profile.user_id')
            ->select('profile.user_id','profile.fullname','profile.profile_picture')
            ->orderBy('profile.fullname', 'desc')
            ->paginate(5);

        return Response::json(array('status' => 'success', 'state' => $nerds->toArray()), 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function postCreate()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'user_id'       => 'required|numeric',
            'friend_id'   => 'required|numeric'
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Response::json('failed', 200);
        } else {
            // store
            $friendship = new Frelation;
            $friendship->user_id = Input::get('user_id');
            $friendship->friend_id = Input::get('friend_id');
            $friendship->type = 1;
            $friendship->save();
            return Response::json('success', 200); // or do a redirect with some message that file was uploaded
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
//    public function getShow($id)
//    {
//        // get the nerd
//        $nerd = Comments::where('id','=',$id)
//                    ->join('profile', 'comments.user_id', '=', 'profile.user_id')
//                    ->select('comments.id','comments.video_id','comments.user_id','comments.text','comments.created_at','profile.fullname','profile.profile_picture')
//                    ->get();
//
//        // show the view and pass the nerd to it
//        return Response::json(array('status' => 'success', 'state' => $nerd->toArray()), 200);
//    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function getEdit($id)
    {
        return Response::json(array('status' => 'success', 'state' => "edit page"), 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function postUpdate($id)
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function postDestroy()
    {
        // delete
        $nerd = Frelation::where('user_id','=',Input::get('user_id'))->where('friend_id','=',Input::get('friend_id'));
        $nerd->delete();

        // redirect
        return Response::json('success', 200);
    }
}